<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * MY_Input
 *
 * @author Simon Emms <simon@simonemms.com>
 */
class MY_Input extends CI_Input {
    
    
    
    
    
    
    
        /**
	 * Constructor
	 *
	 * Sets whether to globally enable the XSS processing
	 * and whether to allow the $_GET array
         * 
         * Changes the security and UTF8 classes from global
         * variables
	 */
	public function __construct() {
            log_message('debug', "Input Class Initialized");

            $this->_allow_get_array	= (config_item('allow_get_array') === TRUE);
            $this->_enable_xss		= (config_item('global_xss_filtering') === TRUE);
            $this->_enable_csrf		= (config_item('csrf_protection') === TRUE);

            $SEC =& load_class('Security', 'core');
            $this->security =& $SEC;

            // Do we need the UTF-8 class?
            if (UTF8_ENABLED === TRUE) {
                $UNI =& load_class('Utf8', 'core');
                $this->uni =& $UNI;
            }

            // Sanitize global arrays
            $this->_sanitize_globals();
	}







    /**
     * REST
     *
     * Extends the input class so that you can get
     * RESTful input.  Returns either the value or
     * false
     *
     * @param string $index
     * @param bool $xss_clean
     * @return array/string/false
     */
    function rest($index, $xss_clean = false, $url = false) {
        $objRouter = &load_class('Router');
        $objUri = &load_class('URI');
        $objConfig = &load_class('Config', 'core');
        $objLoader = &load_class('Loader', 'core');
        $objLoader->helper('html');
        $objConfig->load('rest');

        /* Are we checking the current_url or specified one? */
        if($url !== false) {
            /* Specified - extract the domain */
            $url = preg_replace('/^'.preg_quote(base_url(), '/').'/', '', $url);
            $arrUri = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $url));
            /* Add the domain back in */
            $url = site_url($url);
        } else {
            /* Current */
            $arrUri = $objUri->segment_array();
            $uri = $objConfig->site_url($objUri->uri_string());
        }

        /* Only pay attention after the method */
        $arrUri = array_slice($arrUri, array_search($objRouter->method, $arrUri));

        $queryId = array_search($index, $arrUri);
        if($queryId !== false) {
            /* The next one along is the REST value */
            $valueId = $queryId + 1;
            if(array_key_exists($valueId, $arrUri)) {
                $value = urldecode($arrUri[$valueId]);
                /* Do we have to clean out the filth */
                if($xss_clean) {
                    $value = $this->_fetch_from_array($arrUri, $valueId, $xss_clean);
                }
                /* Check for any multiple values */
                $arrValue = explode($objConfig->item('rest_merge'), $value);
                if(count($arrValue) > 1) {
                    /* Return array */
                    return $arrValue;
                } else {
                    /* Return string */
                    return $value;
                }
            }
        }
        return false;
    }







    /**
     * REST Replace
     *
     * Replaces the REST value in the URL.  Takes the
     * old value and puts the new value in there.  You
     * can specify the URL (useful if it's coming via
     * an AJAX request) or base it on the current_url()
     *
     * @param string $index
     * @param string $value
     * @param string $url
     * @return string
     */
    function rest_replace($index, $value, $url = false) {
        $objRouter = &load_class('Router');
        $objUri = &load_class('URI');

        /* Are we checking the current_url or specified one? */
        if($url !== false) {
            /* Specified - extract the domain */
            $url = preg_replace('/^'.preg_quote(base_url(), '/').'/', '', $url);
            $arrUri = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $url));
            /* Add the domain back in */
            $url = site_url($url);
        } else {
            /* Current */
            $arrUri = $objUri->segment_array();
            $url = current_url();
        }

        $original = $this->rest($index, false, $url);

        if($original !== false) {
            /* Something's already set */
            $queryId = array_search($index, $arrUri);
            unset($arrUri[$queryId]);
            unset($arrUri[$queryId + 1]);

            $url = site_url(implode('/', $arrUri));
        }
        /* We've got the URL less the REST key - only set if not false or null */
        if(!is_null($value) && $value !== false) {
            $url .= '/'.$index.'/';
            $url .= urlencode($value);
        }
        return remove_slash($url);
    }






    /**
     * REST Remove
     *
     * Removes a rest index from the URL
     * 
     * @param string $index
     * @param string $url
     * @return string
     */
    public function rest_remove($index, $url = false) {
        $objUri = &load_class('URI');

        /* Are we checking the current_url or specified one? */
        if($url !== false) {
            /* Specified - extract the domain */
            $url = preg_replace('/^'.preg_quote(base_url(), '/').'/', '', $url);
            $arrUri = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $url));
            /* Add the domain back in */
            $url = site_url($url);
        } else {
            /* Current */
            $arrUri = $objUri->segment_array();
            $url = current_url();
        }

        $queryId = array_search($index, $arrUri);

        /* Remove the index and value */
        if($queryId !== false) {
            unset($arrUri[$queryId]);
            unset($arrUri[$queryId + 1]);
        }

        /* Return the URL */
        return site_url(implode('/', $arrUri));
    }






    /**
     * REST Filter
     *
     * Prepares the URL so can be used on a filter
     * function - removes the value from URL if same
     * value given
     *
     * @param string $index
     * @param string $value
     * @param string $url
     * @return string
     */
    public function rest_filter($index, $value, $url = false) {
        $objRouter = &load_class('Router');
        $objUri = &load_class('URI');
        $objConfig = &load_class('Config', 'core');

        $ignore = false;

        /* Are we checking the current_url or specified one? */
        if($url !== false) {
            /* Specified - extract the domain */
            $url = preg_replace('/^'.preg_quote(base_url(), '/').'/', '', $url);
            $arrUri = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $url));
            /* Add the domain back in */
            $url = site_url($url);
        } else {
            /* Current */
            $arrUri = $objUri->segment_array();
            $url = current_url();
        }

        /* Convert the value to a string - can cock up if an integer */
        $value = (string) $value;

        $original = $this->rest($index, false, $url);

        if($original != false) {
            /* Something's already set */
            $queryId = array_search($index, $arrUri);

            $old_value = urldecode($arrUri[$queryId + 1]);

            $separator = $objConfig->item('rest_merge');

            if($old_value != $value) {
                $new_value = '';
                if(is_array($original)) {
                    $removeId = array_search($value, $original);

                    if($removeId !== false) {
                        unset($original[$removeId]);
                    }
                    $new_value .= implode($separator, $original);

                    /* Clean the values for the regex */
                    $arrRegex = array(
                        'separator' => preg_quote($separator, '/'),
                        'value' => preg_quote($value, '/'),
                    );

                    /* Check it's not already in the URL */
                    if(!preg_match('/(^|'.$arrRegex['separator'].')'.$arrRegex['value'].'('.$arrRegex['separator'].'|$)/i', $old_value)) {
                        /* If value not in URL, don't add to the value */
                        $new_value .= $separator;
                        $new_value .= $value;
                    }

                } else {
                    $new_value .= $old_value;
                    $new_value .= $separator;
                    $new_value .= $value;
                }

                $value = $new_value;
            } else {
                $ignore = true;
            }
            /* Remove the index */
            unset($arrUri[$queryId]);
            unset($arrUri[$queryId + 1]);

            $url = site_url(implode('/', $arrUri));
        }
        /* We've got the URL less the REST key - only set if not false or null */
        if($ignore === false && !is_null($value) && $value !== false) {
            $url .= '/'.$index.'/';
            $url .= urlencode($value);
        }
        return remove_slash($url);
    }
    
}

?>